*****************************************************************************************************************************************************************************************
**Find address on day****
cd "E:\workdata\704429\Projektfiles\Co-residence"
foreach num of numlist 1/2 {
use "E:\workdata\704429\Projektfiles\Co-residence\bopa07_14.dta", clear
rename pnr pnr`num'
merge m:1 pnr`num' using analysis_out_data_all_pairs, keepusing(dato)
keep if _m==3
keep if BOP_VFRA<=dato & BOP_VTIL>=dato
tostring kom, replace
gen adr_now`num'=kom+bopikom
keep pnr`num' adr_
save adr_now`num'.dta, replace
}

**Find address prev. day****
cd "E:\workdata\704429\Projektfiles\Co-residence"
foreach num of numlist 1/2 {
use "E:\workdata\704429\Projektfiles\Co-residence\bopa07_14.dta", clear
rename pnr pnr`num'
merge m:1 pnr`num' using analysis_out_data, keepusing(dato)
keep if _m==3
replace dato=dato-1
keep if BOP_VFRA<=dato & BOP_VTIL>=dato
tostring kom, replace
gen adr_prev`num'=kom+bopikom
keep pnr`num' adr_
save adr_prev`num'.dta, replace
}

use adr_prev2, clear
duplicates drop pnr, force // Drops 1 obs with error in dates
save adr_prev2, replace

***Merge**
cd "E:\workdata\704429\Projektfiles\Co-residence"
est clear
use analysis_out_data_all_pairs, clear
merge 1:1 pnr1 using adr_now1
drop _m
merge 1:1 pnr1 using adr_prev1
drop _m
merge 1:1 pnr2 using adr_now2
drop _m
merge 1:1 pnr2 using adr_prev2
drop _m

gen bopstable1=0
replace bopstable1=1 if adr_now1==adr_prev1 & adr_now1!="" & adr_prev1!=""
gen bopstable2=0
replace bopstable2=1 if adr_now2==adr_prev2 & adr_now2!="" & adr_prev2!=""

**Generate stacked datasæt**
gen id=_n
replace koen1 = koen1 - 1 
replace koen2 = koen2 - 1 

save temp_analysis1.dta, replace
foreach var in stemt residentialstability stemte_2009 pnr FOED_DAG koen bopstable  {
	rename `var'1 `var'3
	rename `var'2 `var'1
	rename `var'3 `var'2
}

append using temp_analysis1.dta

lab def koen 0 "male" 1 "female"
lab val koen1 koen
lab val koen2 koen

*Add covariates

*Age*
gen alder_ved_valg1=2013-year(FOED_DAG1)
replace alder_ved_valg1=alder_ved_valg1-1 if month(FOED_DAG1)==12
replace alder_ved_valg1=alder_ved_valg1-1 if month(FOED_DAG1)==11 & day(FOED_DAG1)>19

gen alder_ved_valg2=2013-year(FOED_DAG2)
replace alder_ved_valg2=alder_ved_valg2-1 if month(FOED_DAG2)==12
replace alder_ved_valg2=alder_ved_valg2-1 if month(FOED_DAG2)==11 & day(FOED_DAG2)>19

gen HH_age=(alder_ved_valg1+alder_ved_valg2)/2

*****************************************************************************************************************************************************************************************
****************************************************************************Merge with other datasets************************************************************************************
*Get ethnicity*
foreach num of numlist 1/2 {
rename pnr`num' pnr
merge 1:1 pnr using "E:\workdata\704429\Grunddatanewest\bef2014.dta", keepusing(IE_TYPE civst)
drop if _m==2
drop _m
rename IE_TYPE IE_TYPE`num'
rename civst civst`num'
rename pnr pnr`num'
}

*Get education*
foreach num of numlist 1/2 {
rename pnr`num' pnr
merge 1:1 pnr using "E:\workdata\704429\Grunddatanewest\udda2014.dta", keepusing(hfaudd_hovedgruppe)
drop if _m==2
drop _m
rename hfaudd_hovedgruppe hfaudd_hovedgruppe`num'
rename pnr pnr`num'
}

*Get income*
foreach num of numlist 1/2 {
rename pnr`num' pnr
merge 1:1 pnr using "E:\workdata\704429\Grunddatanewest\indh2013.dta", keepusing(perindkialt)
drop if _m==2
drop _m
rename perindkialt perindkialt`num'
rename pnr pnr`num'
}


*Minor recodes*
replace stemte_20091 = 2 if stemte_20091 == .
replace stemte_20092 = 2 if stemte_20092 == .

* Estimate the effect only in the month around the cutoff 
eststo clear

gen month_factor=month13+83.5  

eststo: reg stemt1 b84.month_factor  ///
if month13_factor >= 83 & month13_factor <= 84 & bopstable1 == 1, cluster(id) 

eststo: reg stemt1 b84.month_factor  ///
i.IE_TYPE1 i.hfaudd_hovedgruppe1 koen1 i.civst1 i.stemte_20091 ///
c.alder_ved_valg1##c.alder_ved_valg1##c.alder_ved_valg1 ///
c.valg13##i.treatment if bopstable1 == 1, cluster(id) 

eststo: reg stemt1 b85.month_factor  ///
if month13_factor >= 82 & month13_factor <= 85 & bopstable1 == 1, cluster(id) 

eststo: reg stemt1 b85.month_factor  ///
i.IE_TYPE1 i.hfaudd_hovedgruppe1 koen1 i.civst1 i.stemte_20091 ///
c.alder_ved_valg1##c.alder_ved_valg1##c.alder_ved_valg1 ///
c.valg13##i.treatment if bopstable1 == 1, cluster(id) 

esttab est1 est2 est3 est4  , nostar replace ci b(3)

* find N 

log using move_in_graphs/extra.log, append
tab month_factor if month13_factor >= 82 & month13_factor <= 85 & bopstable1==1, sort // min. N Table 3B - stayers
tab month_factor if month13_factor >= 82 & month13_factor <= 85 & bopstable1==1 & ///
IE_TYPE1 != . & hfaudd_hovedgruppe1 != . & koen1 != . & civst1 != . & stemte_20091 != . &  ///
alder_ved_valg1 != . , sort // min. N Table 3B - stayers

log close

*** Find minimum N

